﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace CH.Easy.OpenLogin
{
    public class SinaWeiboOpenAPI : AbstractOpenAPI
    {
        public SinaWeiboOpenAPI(AbstractOauthV2 oAuth)
            : base(oAuth)
        {

        }


        public override string GetAuthorizeUrl()
        {
            return base.oAuth.GetAuthorizeUrl();
        }

        public override AccessTokenDTO GetAccessToken(string code)
        {
            return base.oAuth.GetAccessToken(code);
        }

        public override CallBackDTO GetOpenID(string accessToken)
        {
            return base.oAuth.GetOpenID(accessToken);
        }


        public override UserDTO GetUserInfo(string format)
        {
            //每个API调用必须传的参数
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"uid",base.oAuth.OpenID}
            };
            string url = "https://api.weibo.com/2/users/show.json";
            string json =base.oAuth.OpenRequest.HttpGet(url, param.GetQueryString());
            UserDTO userDTO = new UserDTO();
            userDTO.json = json;
            var sinaWeibo = new
            {
                id = "",
                screen_name = "",
                name = "",
                location = "",
                profile_image_url = "",
                avatar_large = "",
                created_at = "",
                gender = ""
            };
            var sinaWeiboResult = JsonConvert.DeserializeAnonymousType(json, sinaWeibo);
            string[] addressArry = null;
            try
            {
                addressArry = sinaWeiboResult.location.Split(' ');
                userDTO.province = addressArry[0];
                userDTO.city = addressArry[1];

            }
            catch (Exception)
            {
                userDTO.province = "";
                userDTO.city = "";

            }


            userDTO.birth_day = DateTime.Now.Day.ToString();
            userDTO.birth_month = DateTime.Now.Month.ToString();
            userDTO.birth_year = DateTime.Now.Year.ToString();
            //100px，50px，30px
            userDTO.head_big = sinaWeiboResult.avatar_large;
            userDTO.head_medium = sinaWeiboResult.avatar_large;
            userDTO.head_small = sinaWeiboResult.profile_image_url;
            userDTO.name = sinaWeiboResult.name;
            userDTO.nick = sinaWeiboResult.screen_name;
            userDTO.openid = sinaWeiboResult.id;
            //性别，m：男、f：女、n：未知 
            //这里统一性别
            if ("f".Equals(sinaWeiboResult.gender))
            {
                userDTO.sex = "0";//小美女
            }
            else if ("m".Equals(sinaWeiboResult.gender))
            {
                userDTO.sex = "1";//小帅哥
            }
            else
            {
                userDTO.sex = "2";//保密
            }
            userDTO.is_succeed = true;
            userDTO.open_type = base.oAuth.OpenType;
            return userDTO;
        }

        public override ResultDTO AddWeibo(string format, string content, string clientip, string jing, string wei, string syncflag)
        {
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"status",Uri.EscapeDataString(content)}
            };
            string url = "https://api.weibo.com/2/statuses/update.json";
            return base.oAuth.OpenRequest.HttpPost(url, param.GetQueryString()).JsonToObject(base.oAuth.OpenType);
        }

        public override ResultDTO AddPicWeiBo(string format, string content, string clientip, string jing, string wei, string picUrl, string syncflag)
        {

            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"status",Uri.EscapeDataString(content)}
            };

            string url = "https://upload.api.weibo.com/2/statuses/upload.json";
            return base.oAuth.OpenRequest.HttpPostWithImage(url, param, picUrl).JsonToObject(base.oAuth.OpenType);
        }
    }
}
